/*
 * @Author: xiaosihan
 * @Date: 2022-08-20 14:50:15
 * @Last Modified by: xiaosihan
 * @Last Modified time: 2024-05-06 02:16:14
 */
import { autorun } from 'mobx';
import { AmbientLight, DirectionalLight, PointLight, SRGBColorSpace, Vector3 } from 'three';
import ThreeBase from 'three-base';
import editorStore from './editorStore';
import preview3DRenderer from './preview3DRenderer';
import threeUtils from 'three-base/threeUtils';

class Thumb3DRenderer extends ThreeBase {
  constructor() {
    super();
    window.thumb3DRenderer = this;
    this.setCameraType('orthographicCamera');
  }

  // 环境光
  ambientLight = (() => {
    const ambientLight = new AmbientLight('#fff', 1);
    this.scene.add(ambientLight);
    return ambientLight;
  })();

  // 灯位1
  directionalLight1 = (() => {
    const directionalLight = new DirectionalLight('#fff', 0.6);
    directionalLight.position.set(0, 50, 0);
    directionalLight.target.position.set(0, 0, 0);
    this.scene.add(directionalLight);
    return directionalLight;
  })();

  dispose = autorun(
    () => {
      this.needRender = editorStore.needUpdateTime > 0;
    },
    { delay: 30 },
  );

  setSize(width: number, height: number): void {
    super.setSize(width, height);

    Object.assign(this.orthographicCamera, {
      left: -5,
      bottom: -5,
      top: 5,
      right: 5,
    });
  }

  init() {
    this.gridHelper.visible = false;
    this.axesHelper.visible = false;
    this.renderer.outputColorSpace = SRGBColorSpace;
    this.orthographicCamera.far = 5010;
    this.orthographicCamera.near = 4990;
    this.orthographicCamera.updateProjectionMatrix();
    super.init();
    this.setSize(512, 512);
    this.controls.enablePan = false;
    this.autoRender = false;
    this.resetLookAt({
      center: new Vector3(0, 0, 0),
      position: new Vector3(0, 0, 7),
    });
  }

  // 获取缩略图
  async getThumbFile() {
    let blob = new Blob();

    while (blob.size < 5000) {
      this.resetLookAt({
        center: new Vector3(0, 0, 0),
        position: new Vector3(0, 0, 7),
      });

      this.renderer.setRenderTarget(this.baseRenderTarget);
      this.renderer.clear();
      this.renderer.render(preview3DRenderer.scene, this.getCamera());
      this.renderer.setRenderTarget(null);
      this.renderer.clear();
      this.renderRendererTarget(this.baseRenderTarget);
      await new Promise((resolve) => requestAnimationFrame(resolve));
      blob = await new Promise<Blob>((resolve, reject) => {
        this.renderer.domElement.toBlob(
          (blob) => {
            resolve(blob as Blob);
          },
          'image/png',
          1,
        );
      });
    }

    const file = new File([blob], 'test.png', { type: blob.type });
    return file;
  }

  // 获取背面的缩率图
  async getBackThumbFile() {
    let blob = new Blob();

    while (blob.size < 5000) {
      this.resetLookAt({
        center: new Vector3(0, 0, 0),
        position: new Vector3(0, 0, -7),
      });

      this.renderer.setRenderTarget(this.baseRenderTarget);
      this.renderer.clear();
      this.renderer.render(preview3DRenderer.scene, this.getCamera());
      this.renderer.setRenderTarget(null);
      this.renderer.clear();
      this.renderRendererTarget(this.baseRenderTarget);
      await new Promise((resolve) => requestAnimationFrame(resolve));

      blob = await new Promise<Blob>((resolve, reject) => {
        this.renderer.domElement.toBlob(
          (blob) => {
            resolve(blob as Blob);
          },
          'image/png',
          1,
        );
      });
    }

    const file = new File([blob], 'test.png', { type: blob.type });
    return file;
  }

  render(): void {
    super.render();
  }
}

const thumb3DRenderer = new Thumb3DRenderer();

export default thumb3DRenderer;
